<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>车辆保险管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .selected-box {
            background-color: #e6f7ff;
            border: 1px solid #91d5ff;
            padding: 10px 15px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!-- 搜索表单 -->
        <div style="margin: 30px 10px 20px 10px">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item" style="display: flex; align-items: center; flex-wrap: wrap;">
                    <div class="layui-inline" style="margin-bottom: 0; display: flex; align-items: center; margin-right: 10px;">
                        <label class="layui-form-label" style="width:auto; padding:0 15px; border:none; background:transparent; height:36px; line-height:36px;">车牌号</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <input type="text" name="license" class="layui-input" placeholder="请输入车牌号" style="border-radius:4px;">
                        </div>
                    </div>
                    <div class="layui-inline" style="margin-bottom: 0; display: flex; align-items: center;">
                        <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="data-search-btn" style="border-radius:4px; margin-right:5px;">
                            <i class="layui-icon">&#xe615;</i>查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-normal layui-btn-sm" style="border-radius:4px; margin-right:5px;">
                            <i class="layui-icon">&#xe669;</i>重置
                        </button>
                        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add" style="border-radius:4px; margin-right:5px;">
                            <i class="layui-icon">&#xe654;</i>新增保险
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <!-- 批量操作区域 -->
        <div class="selected-box" style="margin: 0 10px 15px 10px; display: flex; align-items: center;">
            <span id="selected-count" style="margin-left: 10px;">已选择0项</span>
            <div style="width: 20px;"></div>
            <button class="layui-btn layui-btn-sm" id="batch-delete-btn" style="background-color: transparent; color: #1E9FFF; font-weight: bold; font-size: 14px;">清空</button>
        </div>

        <!-- 表格容器 -->
        <div style="margin: 0 10px">
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>

        <!-- 表格操作按钮模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 编辑/添加弹窗 -->
        <div id="edit" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="editForm">
                <div class="layui-form-item" id="id-laylabel">
                    <label class="layui-form-label required">保险ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" placeholder="保险ID" readonly value="" class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">车牌号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="license" lay-verify="required" lay-reqtext="车牌号不能为空" placeholder="请输入车牌号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">保险公司</label>
                    <div class="layui-input-inline">
                        <input type="text" name="insuraName" placeholder="请输入保险公司名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商业险保单号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="insurancePolicy" placeholder="请输入商业险保单号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商业险日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="insuranceDateTime" placeholder="请输入商业险日期" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">交强险保单号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="strongPolicy" placeholder="请输入交强险保单号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">交强险日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="strongDateTime" placeholder="请输入交强险日期" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var index;
        var selectedIds = [];

        // 表格渲染
        table.render({
            elem: '#currentTableId',
            url: '/insurance/queryPage',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 100, title: '保险ID', sort: true},
                {field: 'license', width: 100, title: '车牌号'},
                {field: 'insuraName', width: 160, title: '保险公司'},
                {field: 'insurancePolicy', width: 120, title: '商业险保单号'},
                {field: 'insuranceDateTime', width: 200, title: '商业险日期'},
                {field: 'strongPolicy', width: 120, title: '交强险保单号'},
                {field: 'strongDateTime', width: 200, title: '交强险日期'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done: function(res, curr, count) {
                // 每次表格重新渲染后清空选择
                selectedIds = [];
                updateSelectedCount();
            }
        });

        // 复选框选择监听
        table.on('checkbox(currentTableFilter)', function(obj){
            var data = obj.data;
            if(obj.checked){
                selectedIds.push(data.id);
            } else {
                selectedIds = selectedIds.filter(function(id){
                    return id !== data.id;
                });
            }
            updateSelectedCount();
        });

        // 更新已选择数量显示
        function updateSelectedCount() {
            $('#selected-count').text('已选择' + selectedIds.length + '项');
        }

        // 批量删除按钮点击事件
        $('#batch-delete-btn').click(function(){
            if(selectedIds.length === 0) {
                layer.msg('请至少选择一项');
                return;
            }

            layer.confirm('确定删除选中的' + selectedIds.length + '项数据吗？', function(index){
                $.ajax({
                    url: '/insurance/batchDelete',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(selectedIds),
                    success: function(res) {
                        if(res.code === 0) {
                            layer.msg('删除成功');
                            table.reload('currentTableId');
                        } else {
                            layer.msg(res.msg || '删除失败');
                        }
                    },
                    error: function(xhr) {
                        layer.msg('请求失败: ' + xhr.responseText);
                    }
                });
                layer.close(index);
            });
        });

        // 搜索监听
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {curr: 1},
                where: data.field
            }, 'data');
            return false;
        });

        // 新增保险功能
        function addInsurance() {
            $("#id-laylabel").hide();
            form.val("editForm", {
                id: '',
                license: '',
                insuraName: '',
                insurancePolicy: '',
                insuranceDateTime: '',
                strongPolicy: '',
                strongDateTime: ''
            });
            index = layer.open({
                title: '添加保险',
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['40%', '80%'],
                content: $("#edit"),
                cancel: function() {
                    index = null;
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        // 监听新增按钮点击
        $(document).on('click', 'button[lay-event="add"]', function(e) {
            e.preventDefault();
            e.stopPropagation();
            addInsurance();
            return false;
        });

        // 行内按钮监听 - 编辑 & 删除
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $("#id-laylabel").show();
                form.val("editForm", data);
                index = layer.open({
                    title: '编辑保险',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '80%'],
                    content: $("#edit")
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该保险信息？', function (index) {
                    $.getJSON("/insurance/delete?id=" + data.id, function (res) {
                        if (res.code === 0) {
                            layer.msg("删除成功");
                            obj.del();
                        } else {
                            layer.msg("删除失败");
                        }
                    });
                    layer.close(index);
                });
            }
        });

        // 表单提交监听
        form.on('submit(saveBtn)', function (data) {
            var field = data.field;
            var url = field.id ? "/insurance/update" : "/insurance/add";
            $.post(url, field, function (res) {
                if (res.code === 0) {
                    layer.msg(field.id ? "更新成功" : "添加成功");
                    table.reload('currentTableId');
                    layer.close(index);
                } else {
                    layer.msg(field.id ? "更新失败" : "添加失败");
                }
            });
            return false;
        });
    });
</script>
</body>
</html>